package com.example.admin_test.emp.controller;

import com.example.admin_test.common.vo.Result;
import com.example.admin_test.emp.pojo.Emp;
import com.example.admin_test.emp.service.EmpService;
import com.example.admin_test.emp.vo.EmpQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/emp")
public class EmpController {
    @Autowired
    EmpService empService;

    @Resource
    Result result;

    @GetMapping("")
    public String empList(){
        return "emp/empList";
    }

    @GetMapping("/list")
    @ResponseBody
    public Result<Object> getEmpList(EmpQuery param){
        List<Emp> list=empService.getEmpList(param);
        Long count=empService.countEmpList(param);
        return result.success(list,count);
    }

    @PostMapping("")
    @ResponseBody
    public Result<Object> addEmp(Emp emp){
        empService.addEmp(emp);
        return result.success("添加成功");
    }

    @DeleteMapping("/{ids}")
    @ResponseBody
    public Result<Object> deleteEmpByIds(@PathVariable("ids") String ids){
        empService.deleteEmpByIds(ids);
        return result.success("删除成功");
    }
    
    @GetMapping("/{id}")
    public String getEmpById(@PathVariable("id") Integer id, Model model){
        Emp emp = empService.empById(id);
        model.addAttribute("empEdit",emp);
        model.addAttribute("deptList",empService.getAllDept());
        return "emp/empEdit";
    }

    //修改员工
    @PutMapping("")
    @ResponseBody
    public Result<Object> updateEmp(Emp emp){
        empService.updateEmp(emp);
        return result.success("修改员工成功");
    }
}
